/**
 * 
 */
//TODO
//agregar funciones de: 
	//http://jsfiddle.net/zalun/FZqZN/
	//http://jsfiddle.net/BaG4J/5/	


//ver http://jsfiddle.net/BaG4J/2/
  function printAt( context , text, x, y, lineHeight, fitWidth)
  {
      fitWidth = fitWidth || 0;
      
      if (fitWidth <= 0)
      {
           context.fillText( text, x, y );
          return;
      }
      
      for (var idx = 1; idx <= text.length; idx++)
      {
          var str = text.substr(0, idx);
         // console.log(str, context.measureText(str).width, fitWidth);
          if (context.measureText(str).width > fitWidth)
          {
              context.strokeText( text.substr(0, idx-1), x, y );
              context.fillText( text.substr(0, idx-1), x, y );
              printAt(context, text.substr(idx-1), x, y + lineHeight, lineHeight,  fitWidth);
              return;
          }
      }
      context.fillText( text, x, y );
  }
  
  
  //le agregue el parametro 'imprimir'. si es verdadero: imprimo sobre el canvas. si es falso: sirve para sacar las dimensiones del texto
  function printAtWordWrap( context , text, x, y, lineHeight, fitWidth, imprimir)
  {
	  
      fitWidth = fitWidth || 0;
      
      if (fitWidth <= 0)
      {
    	  if (imprimir) context.fillText( text, x, y );
          return;
      }
      var words = text.split(' ');
      var currentLine = 0;
      var idx = 1;
      var maxWidth = 0;
      var palabraCortada = false;
      while (words.length > 0 && idx <= words.length)
      {
          var str = words.slice(0,idx).join(' ');
          var w = context.measureText(str).width;
//          maxWidthAnterior = maxWidth;
          
         
          if ( w > fitWidth )
          {
              if (idx==1)//hay una palabra que excede el ancho total (se va a ver cortada)
              {
            	  palabraCortada = true;
                  idx=2;
              }
              if (imprimir) context.fillText( words.slice(0,idx-1).join(' '), x, y + (lineHeight*currentLine) );
              currentLine++;
              words = words.splice(idx-1);
              idx = 1;
          }
          else
          {
        	  maxWidth = Math.max(maxWidth, w);
        	  idx++;
        	 }
      }
      if  (idx > 0)
    	  if (imprimir)  context.fillText( words.join(' '), x, y + (lineHeight*currentLine) );
     
      if (debug){
    	  console.log("lineHeight",lineHeight);
    	  console.log("currentLine",currentLine);
      }
      
      
      return {ancho: maxWidth, alto: (lineHeight*(1+currentLine)+y), palabraCortada: palabraCortada};
      
  }